package com.delong.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.model.entity.SystemPost;
import com.delong.lease.model.entity.SystemUser;
import com.delong.lease.web.admin.mapper.SystemPostMapper;
import com.delong.lease.web.admin.mapper.SystemUserMapper;
import com.delong.lease.web.admin.service.SystemPostService;
import com.delong.lease.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delong.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.delong.lease.web.admin.vo.system.user.SystemUserQueryVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemPostMapper systemPostMapper;

    @Override
    public void pageList(Page<SystemUserItemVo> systemUserItemVoPage, SystemUserQueryVo queryVo) {
        systemUserMapper.pageList(systemUserItemVoPage,queryVo);
    }

    @Override
    public SystemUserItemVo getUserDetailById(Long id) {
        //查询系统用户基本信息
        SystemUser systemUser = systemUserMapper.selectNoPasswordById(id);
        if (systemUser == null){
            throw new LeaseException(ResultCodeEnum.FAIL);
        }
        //查询系统岗位信息
        SystemPost systemPost = systemPostMapper.selectById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(systemUser,systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());
        return systemUserItemVo;
    }

    @Override
    public void SystemUserSaveOrUpdate(SystemUser systemUser) {
        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //保存
        //查询数据库中是否有相同用户名的用户
        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername, systemUser.getUsername());
        //修改
        //查询数据库中是否有相同除了当前修改用户的用户名
        systemUserLambdaQueryWrapper.ne(systemUser.getId() != null, SystemUser::getId, systemUser.getId());

        long count = count(systemUserLambdaQueryWrapper);
        if (count > 0){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        if (!ObjectUtils.isEmpty(systemUser.getPassword())){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        saveOrUpdate(systemUser);
    }


}




